<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Home extends CI_Controller {

        function __construct(){
            parent::__construct();
            $this->data['page'] = 'Home';
        }

        public function index()
        {
            $this->data = array(
                'title' => 'Dashboard',
                /*'objects' => Model\Objects::result()->all(),
                'owners' => Model\Owners::all(),
                'attributes' => Model\Attributes::all(),*/
            );

            //$this->firephp->log($this->data['objects']->to_json());
            $this->template->build('home/home.php', $this->data);
        }

        

        public function getMyRowsObject()
        {
            $result = $this->_jsonTable('objects',array('ID','type','name','status','date_time','owners_ID','fields'));
            echo json_encode($result);
        }
        
        public function getMyRowsOwners()
        {
            $result = $this->_jsonTable('owners',array('ID','name','email','telephone'));
            echo json_encode($result);
        }
        
        public function getMyRowsAttributes()
        {
            $result = $this->_jsonTable('attributes',array('ID','value','name','required','custom'),'WHERE custom=1');
            echo json_encode($result);
        }

        private function _jsonTable($table,$rows,$where = '')
        {
            $page = $_GET['page']; // get the requested page 
            $limit = $_GET['rows']; // get how many rows we want to have into the grid 
            $sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
            $sord = $_GET['sord']; // get the direction 

            if(!$sidx) $sidx =1;

            // connect to the database  
            $result = mysql_query("SELECT COUNT(*) AS count FROM $table $where"); 
            $row = mysql_fetch_array($result,MYSQL_ASSOC); 
            $count = $row['count']; 

            if( $count >0 ) 
            { 
                $total_pages = ceil($count/$limit); 
            } 
            else 
            { $total_pages = 0; } 

            if ($page > $total_pages) $page=$total_pages; 
            $start = $limit*$page - $limit; // do not put $limit*($page - 1) 

            $SQL = "SELECT * FROM $table $where ORDER BY $sidx $sord LIMIT $start , $limit"; 
            $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error()); 

            $responce->page = $page; 
            $responce->total = $total_pages; 
            $responce->records = $count; 

            $i=0; 
            while($row = mysql_fetch_array($result,MYSQL_ASSOC)) 
            { 
                
                $data = array();
                
                for($j=0;$j<=count($rows)-1;$j++){
                   $data[] = $row[$rows[$j]];
                }
                
                //$data[] = 'Edit';
                
                $url = base_url($table.'/view_edit/'.$row['ID']);
                $link1 = "<a href='#' onclick='editObjectDialog(\"$url\"); return false;'>Edit</a>"; 
                
                //$this->firephp->log($link1);
                
                $data[] = $link1;
                
                $responce->rows[$i]['ID']=$row[$rows[0]];
                
                
                 
                $responce->rows[$i]['cell']=$data; 
                $i++; 
            } 

           return $responce;
        }
    }

    /* End of file Home.php */
    /* Location: ./application/controllers/Home.php */